import service from '@/utils/http';

interface LogModel {
  type: 'success' | 'error';
  detail: string;
  timestamp: number;
}

// http://gotest.minerhub-api.com/v1/monitorminer/log?mid=62&mac=00:30:18:54:08:F8

export function fetchMinerLog(mid: number, mac: string): Promise<LogModel[]> {
  return new Promise(async (resolve, reject) => {
    const res = await service('/monitorminer/log', {
      params: {
        mid,
        mac,
      },
    });
    if (res.data.code === 0) {
      const result = res.data.result;
      const timestamp = result.timestamp;
      const detailList = result.detail.split('\n').filter((d: string) => d);

      resolve(
        detailList.map((d: string) => ({
          type: 'success',
          detail: d,
          timestamp,
        })),
      );
    } else {
      reject();
    }
  });
}
